package com.hongxun.design.utils.selfupdate;

import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.hongxun.design.MyApp;
import com.hongxun.design.bean.SystemVersionBean;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.mytools.VersionUtil;
import com.hongxun.design.ui.dialogstyle.UpdateDialog;
import com.hongxun.design.utils.LoadingUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;

/**
 * Created by Admin on 2017/2/23.
 */

public class UpdateUtil {
    UpdateUtil() {
        addHandler();
    }

    UpdateUtil(Handler mhandler) {
        handler = mhandler;
    }

    private Handler handler;
    Handler sucessHandler;int code;
    static UpdateUtil instence;
    LoadingUtil loadingUtil;
    SystemVersionBean bean;
    public static UpdateUtil getInstence() {
        if (instence == null) {
            instence = new UpdateUtil();
        }
        instence.reInit();
        return instence;
    }

    public static UpdateUtil getInstence(Handler handler) {
        if (instence == null) {
            instence = new UpdateUtil(handler);
        }
        instence.reInit();
        return instence;
    }

    public void reInit(){
        loadingUtil=new LoadingUtil(MyApp.activity);
    }
    public void checkUpdate() {
        this.sucessHandler=null;
        loadingUtil.start("请稍等...");
        MyApp.HTTP.getCheckUpdateVersion(handler);
    }

    public void checkUpdate(Handler sucessHandler,int code) {
        this.sucessHandler=sucessHandler;
        this.code=code;
        loadingUtil.start("请稍等...");
        MyApp.HTTP.getCheckUpdateVersion(handler);
    }

    void addHandler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        if (FastJsonUtil.JsonToCheck((String) msg.obj)){
                            loadingUtil.success();
                            bean= FastJsonUtil.JsonToSystemVersionBean((String) msg.obj);
                            if (bean==null){
                                ToastShow.showShort(MyApp.activity,"服务器返回数据为空！");
                                return;
                            }
                            String versionName = VersionUtil.getVersionName(MyApp.activity);
                            if (bean.getVersion() != null && bean.getVersion().length() > 0
                                    && !bean.getVersion().equals(versionName) && check(bean.getVersion())) {
                                showUpdateDialog(bean);
                            } else {
                                if (code==0) ToastShow.showShort(MyApp.activity,"暂无更新！");
                                if (sucessHandler!=null) sucessHandler.sendEmptyMessage(code+1);
                            }
                        }else{
                            loadingUtil.error(FastJsonUtil.JsonToCheck_String((String) msg.obj));
                        }
                        break;
                    case 2:
                        break;
                    case HttpStringUtil.ERROR:
                        loadingUtil.error((String) msg.obj);
                        if (sucessHandler!=null) sucessHandler.sendEmptyMessage(code+1);
                        break;
                }
            }
        };
    }

    private boolean check(String code) {
        try {
            String num=code.replace(".","");
            int numCode = Integer.parseInt(num);
            int VersionCode = VersionUtil.getVersionCode(MyApp.activity);
            if (numCode > VersionCode) return true;
        } catch (Exception e) {

        }
        return false;

    }
    UpdateDialog dialog;
    private   void showUpdateDialog(final SystemVersionBean bean){
          dialog = new UpdateDialog(MyApp.activity, bean, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean!=null&&bean.getUpgrade()==1){//是否强制更新1：强制0：不
                }else{
                    if (sucessHandler!=null)sucessHandler.sendEmptyMessage(code);
                }
                dialog.dismiss();
//                APKAnsyTask apkAnsyTask=new APKAnsyTask(MyApp.activity,bean.getDownload_addr());
//                apkAnsyTask.setCallFinishBack(new APKAnsyTask.CallFinishBack() {
//                    @Override
//                    public void Back() {
//                        if (sucessHandler!=null) sucessHandler.sendEmptyMessage(code+1);
//                    }
//                });
//                apkAnsyTask.execute();
                /**跳转到系统下载**/
                Uri uri = Uri.parse(bean.getDownload_addr());
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                MyApp.activity.startActivity(intent);
            }
        } , new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sucessHandler!=null) sucessHandler.sendEmptyMessage(code+1);
            }
        });
//        if (bean!=null&&bean.getUpgrade()==1){//是否强制更新1：强制0：不
            dialog.setCancelable(false);
//        }else{
//            dialog.setCancelable(true);
//        }
        dialog.show();
    }


    public void onDestory(){
        if (loadingUtil!=null)loadingUtil.closeDialogNow();
    }

}
